package itsm.isperp.module.controller.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.report.ReportDescription;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.module.service.release.RlsReleaseService;
import itsm.isperp.module.service.report.ReportCheckinDataService;
import itsm.isperp.module.service.report.ReportDescriptionDetailService;
import itsm.isperp.module.service.report.ReportDescriptionService;
import itsm.isperp.workflow.utils.BpmConst;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("report/reportCheckin")
public class ReportCheckinController extends BaseController<ReportDescription>{
    
    @Autowired
    private ReportCheckinDataService reportCheckinDataService;
    
    @Autowired
    private ReportDescriptionService reportDescriptionService;
    
    @Override
	public ReportDescriptionService getService() {
		return this.reportDescriptionService;
	}

	@Override
	public String getPageTitle() {
		return "工作报告";
	}
    

    @RequestMapping(value = "checkinManage")
    public ModelAndView checkinManage(HttpServletRequest req) {

        ModelAndView mv = new ModelAndView("wechat/v2/checkinManage");
        mv.addObject("pageTitle", "打卡记录");

        return mv;
    }
    
    @RequestMapping(value = "checkinManage1")
    public ModelAndView checkinManage1(HttpServletRequest req) {

        ModelAndView mv = new ModelAndView("wechat/v2/checkinManage1");
        mv.addObject("pageTitle", "打卡记录");

        return mv;
    }

    @RequestMapping("listCheckinDetailPub")
    @ResponseBody
    public PageResponse<Map<String, Object>> listCheckinDetailPub(
            JqGridRequest request, HttpServletRequest req,String checkinType) {
    	String userName = ContextHolder.getLoginUsername();
    	int maxDataRange = 11;
        String sql="SELECT MAX(data_range) r FROM `v_app_user_role` WHERE app_user_name='"+userName+"'";
        JdbcTemplate jdbcTemplate = ContextHolder
                .getSpringBean("jdbcTemplate");
        Integer tr=jdbcTemplate.queryForObject(sql, Integer.class);
        if(tr!=null)
          maxDataRange=tr;
        String permission ="";
        switch(maxDataRange){
        case 1111:
        	permission = "";
        	break;
        default:
        	permission = "fullname='"+userName+"' and ";
        	break;
        }
        
    	if("0".equals(checkinType)){
    		jdbcTemplate.execute("SET @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");
    		request.setSelectColumns("*,count(1) as times,case exception_type when '' then '正常' else exception_type end as status");
        	request.setSearchString(permission+"(checkin_type='上班打卡' or checkin_type='下班打卡')");
        	request.setGroupBy("fullname,date_day");
        }else if("1".equals(checkinType)){
        	request.setSelectColumns("*,case exception_type when '' then '正常' else exception_type end as status");
        	request.setSearchString(permission+"checkin_type='外出打卡'");
        }
        PageResponse<Map<String, Object>> listDetailPub = this.reportCheckinDataService.findAllDetailByRequest(request).toPage();
        return listDetailPub;
    } 
    
    @RequestMapping("checkinView")
	public ModelAndView view(String id, HttpServletRequest req,JqGridRequest request) {
    	 ModelAndView mv = new ModelAndView("wechat/v2/view");
         mv.addObject("pageTitle", "打卡记录");
         
         request.setSearchString("id = '"+id+"'");
         Map<String, Object> result = this.reportCheckinDataService.findAllDetailByRequest(request).get(0);
         String[] temp = ((String)result.get("treenode_name_path")).split("/");
         String departName = temp[temp.length-1];
         
         mv.addObject("departName", departName);
         mv.addObject("name", result.get("fullname"));
         mv.addObject("date", result.get("date_day"));
		return mv;
	}
    
    @RequestMapping("personListDetail")
    @ResponseBody
    public PageResponse<Map<String, Object>> personListDetail(
            JqGridRequest request, HttpServletRequest req,String date,String name) {
    	request.setSelectColumns("*,case exception_type when '' then '正常' else exception_type end as status");
    	request.setSearchString("date_day = '"+date+"' and fullname = '"+name+"'");
    	PageResponse<Map<String, Object>> listDetailPub = this.reportCheckinDataService.findAllDetailByRequest(request).toPage();
    	return listDetailPub;
    }
    
    @RequestMapping("reportView")
   	public ModelAndView reportView(String id, HttpServletRequest req,JqGridRequest request) {
		ModelAndView mv = new ModelAndView("wechat/v2/reportView");
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		ReportDescription model = reportDescriptionService.findOne(id);
		String userName = model.getUserName();
		String sql = "select fullname from app_user where name = '"+userName+"'";
		String name = jdbcTemplate.queryForList(sql, String.class).get(0);
		
		mv.addObject("model", model);
		mv.addObject("name", name);
   		return mv;
   	}
    
    @RequestMapping("listDetailPub")
    @ResponseBody
    public PageResponse<Map<String, Object>> listDetailPub(
            JqGridRequest request, HttpServletRequest req) {
    	JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
    	jdbcTemplate.execute("SET @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");
    	
    	String userName = ContextHolder.getLoginUsername();
    	int maxDataRange = 11;
        String sql="SELECT MAX(data_range) r FROM `v_app_user_role` WHERE app_user_name='"+userName+"'";
        Integer tr=jdbcTemplate.queryForObject(sql, Integer.class);
        if(tr!=null)
          maxDataRange=tr;
        switch(maxDataRange){
        case 1111:
        	break;
        default:
        	request.setSearchString("fullname = '"+userName+"'");
        	break;
        }
        PageResponse<Map<String, Object>> listDetailPub = this.reportDescriptionService.getRepository().findDescriptionDetail(request).toPage();
        return listDetailPub;
    }

}
